package cn.baiweigang.uu.service.mail;

import cn.baiweigang.uu.GlobalContext;
import cn.baiweigang.uu.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;

@Service
@Slf4j
public class MailService {

    private static String smtpHost = "smtp.126.com";
    private static String userName = "bwgang@126.com";
    private static String userPwd ;
    private static String receiver = "bwgang@126.com";

    public void senMail(String subject, String mailBody) {
        //从DB加载发邮件的授权token
        if (StringUtil.isBlank(userPwd)){
            loadPwd();
        }
        MimeMessage mMessage = null;
        Session mailSession = null;
        //1、连接邮件服务器的参数配置附件名称过长乱码解决，关键词false
        System.setProperty("mail.mime.splitlongparameters", "false");
        Properties props = new Properties();
        props.setProperty("mail.smtp.host", smtpHost);
//        props.setProperty("mail.smtp.port", "");
        props.setProperty("mail.smtp.auth", "true");
        // 设置SMTP连接和发送邮件的超时时间，因为缺省是无限超时，单位毫秒
        props.setProperty("mail.smtp.connectiontimeout", "15000");//SMTP服务器连接超时时间
        props.setProperty("mail.smtp.timeout", "60000");//发送邮件超时时间

        MailSMTPAuthenticator smtpAutr = new MailSMTPAuthenticator();
        smtpAutr.setsName(userName);
        smtpAutr.setsPassword(userPwd);
        mailSession = Session.getInstance(props, smtpAutr);

//        mailSession.setDebug(true);
        mMessage = new MimeMessage(mailSession);
        try {
            //发件人
            InternetAddress formAddress = new InternetAddress(userName);
            formAddress.setPersonal("策略筛选结果通知");
            mMessage.setFrom(formAddress);
            //发送时间
            mMessage.setSentDate(new Date());
            //收件人
            InternetAddress[] toAddress = {new InternetAddress(receiver)};
            mMessage.setRecipients(Message.RecipientType.TO, toAddress);
            //主题
            mMessage.setSubject(subject);

            //内容
            MimeBodyPart contentBodyPart = new MimeBodyPart();
            contentBodyPart.setContent(mailBody, "text/html;charset=UTF-8");

            Multipart multipart=new MimeMultipart();
            multipart.addBodyPart(contentBodyPart);
            mMessage.setContent(multipart);
            mMessage.saveChanges();
            Transport.send(mMessage);
//            Transport transport = null;
//            transport = mailSession.getTransport("smtp");
//            transport.connect("", "");
//            transport.sendMessage(mMessage, mMessage.getAllRecipients());
//            transport.close();
        } catch (MessagingException | UnsupportedEncodingException e) {
            log.error(e.getMessage());
        }
    }

    public void loadPwd(){
        userPwd = GlobalContext.dbConfigCache.get("mail_pwd_bwgang")+"";
    }
}
